﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

namespace Client
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public void SubmitReview(object sender, EventArgs e)
        {
            AddReview.Service3 service = new AddReview.Service3();
            int xml = service.AddReview(Name.Text, Request.QueryString["id"], Comments.Text, Int32.Parse(Rating.SelectedValue));
            if (xml == 0)
            {
                Box2.InnerHtml = "<div class='text'>Thank you for your review!</div>";
                int count = Int16.Parse(ReviewCount.InnerText)+1;
                ReviewCount.InnerHtml = (count.ToString());
                Reviews.InnerHtml = getReviewHTML(Name.Text, Comments.Text, (DateTime.Today.Year + "-" + DateTime.Today.Month + "-" + DateTime.Today.Day), Int32.Parse(Rating.SelectedValue), false)
                    + "<hr>" + Reviews.InnerHtml;
            }
            else Box2.InnerHtml = "<div class='text'>Please try later!</div>";
        }
        private string getBusinessName(string name)
        {
            return "<div class='title'>Business Name: </div>" + name;
        }
        private string getReviewHTML(string username, string text, string date, int rating, bool odd)
        {
            string ret = "";
            ret += "<div class='myBox'>";
            ret += "<div class='text'>" + username + " @ " + date + "</div>";
            ret += "<div>";
            for (int i = 1; i <= 5; ++i) {
                if (i <= rating)
                    ret += "<span class='star'></span>";
                else ret += "<span class='star2'></span>";
            }
            ret += "</div>";
            ret += "<div>" + text + "</div>";
            ret += "</div>";
            return ret;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string bid = Request.QueryString["id"];

            if (bid == null)
            {
                // no param
                ErrorMessage.InnerHtml = "<h2>The full profile could not be found.</h2>";
                // Hide the content
                Content.Style.Add("display", "none");
            }
            else
            {
                FullProfile.Service1 service = new FullProfile.Service1();

                // Xml Parser
                string xml = service.FullProfile(bid);
                XmlReader reader = XmlReader.Create(new StringReader(xml));

                string currentElement = "";

                string address = "";
                string spec = "";
                string lon = "0";
                string lat = "0";

                int count = 0;
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            currentElement = reader.Name;
                            if (currentElement.Equals("entry")) 
                            {
                                ++count;
                                string username = "";
                                string text = "";
                                string date = "";
                                int rating = 0;
                                string aux = "";
                                while (reader.Read()) 
                                {
                                    if (reader.NodeType.Equals(XmlNodeType.EndElement) && reader.Name.Equals("entry")) break;
                                    if (reader.NodeType.Equals(XmlNodeType.Element)) {
                                        aux = reader.Name;
                                    }
                                    if (reader.NodeType.Equals(XmlNodeType.Text))
                                    {
                                        if (aux.Equals("username")) username = reader.Value;
                                        if (aux.Equals("text")) text = reader.Value;
                                        if (aux.Equals("date")) date = reader.Value;
                                        if (aux.Equals("rating")) rating = Int16.Parse(reader.Value.ToString());
                                    }
                                }
                                string inner = getReviewHTML(username, text, date, rating, count % 2 != 0);
                                Reviews.InnerHtml += inner + "<hr>";
                            }
                            break;
                        case XmlNodeType.Text:
                            switch (currentElement) 
                            {
                                case "error":
                                    ErrorMessage.InnerHtml = "<h2>The full profile could not be found.</h2>";
                                    // Hide the content
                                    Content.Style.Add("display", "none");
                                    break;
                                case "name":
                                    BusinessName.InnerHtml = getBusinessName(reader.Value);
                                    break;
                                case "address1":
                                    address = reader.Value;
                                    break;
                                case "address2":  
                                case "address3":
                                    address = "<br />" + reader.Value;
                                    break;
                                case "phone":
                                    PhoneNumber.InnerHtml = "<span class='title'>Phone Number: </span>" + reader.Value;
                                    break;
                                case "category":
                                    spec += " " + reader.Value;
                                    break;
                                case "longitude":
                                    lon = reader.Value;
                                    break;
                                case "latitude":
                                    lat = reader.Value;
                                    break;
                            }
                            break;
                    }
                }
                // handle everything
                Address.InnerHtml = "<span class='title'>Address: </span>" + address;
                Spec.InnerHtml = "<span class='title'>Specialties: </span>" + spec;
                string src = "http://maps.google.com/staticmap?center=" + lat + "," + lon + "&zoom=15&size=250x250&maptype=mobile\\&markers=" + lat + "," + lon + ",blue&size=238x245&key=ABQIAAAA-O3c-Om9OcvXMOJXreXHAxSsTL4WIgxhMZ0ZK_kHjwHeQuOD4xSbZqVZW2U_OWOxMp3YPfzZl2GavQ";
                Map.InnerHtml = "<img src='" + src + "' /> ";
                ReviewCount.InnerHtml = count.ToString();
               }
        }
    }
}